<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP '销售订单' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
		<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
		<script type="text/javascript" src="<%=basePath%>/javascript/MQuery.js"></script>

		<script type="text/javascript">
		
		$(document).ready(function() {
			loadCustomer();
			loadDefine();
		  // Handler for .ready() called.
		});
		
		//加载客户列表
		function loadCustomer(){
			MQuery.url = "<%=basePath%>/servlet/CustomerServlet";
			MQuery.data.random = Math.random();
			MQuery.responseFun = getCustomerListResponse;
			MQuery.submit();
			
			function getCustomerListResponse(responseJson) {
				$("#customer").empty();
				$.each(responseJson, function(key, val) {
					$("<option value='"+val.ccusCode+"'"+">"+val.ccusName+"</option>").appendTo("#customer");
				  });
			}
		}
		
		//加载使用单位列表
		function loadDefine(){
			MQuery.url = "<%=basePath%>/servlet/UserDefineServlet";
			MQuery.data.random = Math.random();
			MQuery.responseFun = myResponseFun;
			MQuery.submit();
			
			function myResponseFun(data){
				$("#userdefine").empty();
				$.each(data, function(key, val) {
					$("<option value='"+val.cvalue+"'"+">"+val.cvalue+"</option>").appendTo("#userdefine");
				  });
			}
		}
		
		function addSoSomain(){
			MQuery.url = "<%=basePath%>/servlet/SalesOrderServlet";
			MQuery.data.forward = "addSoSomain";
			MQuery.data.ccusCode = $("#customer").val();  //客户编码 根据客户列表获取
			MQuery.data.ccusName = $("#customer").find("option:selected").text();//客户名称 根据客户列表获取 对应的名称
			MQuery.data.cdefine11 = $("#userdefine").val();//使用单位名称 根据使用单位列表获取
			MQuery.responseFun = addSuccess;
			MQuery.submit();
			
			
			function addSuccess(json){
				//alert("添加成功"+json.message); // 主表的编号和ID, 附表有用 中编号和ID中间用逗号区分
				var ms = json.message.split(",");
				
				MQuery.data.forward = "addSoSodetails";
				
				MQuery.data.cinvName = "订书针21111";//存货名称
				MQuery.data.cinvCode = "1F3352";//存货编码
				MQuery.data.csocode = ms[0]; // 对应主表的编号, 添加主表成功后的返回值;
				MQuery.data.id = ms[1];//主表返回的ID 添加主表成功后的返回值;
				MQuery.data.iquantity = "12";// 数量
				MQuery.data.iquotedPrice = "157.5";//售价
				MQuery.data.iunitPrice = "2.00";//原币无税单价
				MQuery.data.inatUnitPrice = "2.00";//同上
				MQuery.data.itaxUnitPrice = "2.34";//原币含税单价
				MQuery.data.imoney = "10.34";//金额 , 单价* 数量
				MQuery.data.inatMoney = "10.34";//同上
				MQuery.data.itax = "13.001";//税额, 就是 imoney*0.17
				MQuery.data.inatTax = "13.001";//同上
				MQuery.data.isum = "13.001";//价格和税额 合计金额, 就是 itax + itax
				MQuery.data.inatSum = "13.001";//同上
				MQuery.data.irowNo = "2";//子订单按照1,2,3排列顺序号 例如:如果当前订单下再增一个子订单, 序号为2
				
				MQuery.responseFun = addSuccess2;
				MQuery.submit();
				
				function addSuccess2(json){
					alert("添加成功");
				}
				
			}
		}
		
		
	</script>

	</head>

	<body>
		<li>选择客户: <select id="customer"></select>
		<li>选择使用单位: <select id="userdefine"></select>
		<li>
			<input type="button" value="addSoSomain"
				onclick="addSoSomain()">
	</body>
</html>
